﻿using DBHelper;
using DBService;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using TouchSocket.Sockets;
using WpfApp1.ViewModels;
using WpfApp1.Views;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public IServiceProvider ServiceProvider { get; }

        public new static App Current => (App)Application.Current;

        protected App()
        {
            ServiceProvider = ConfigureServices();
        }

        private static ServiceProvider ConfigureServices()
        {
            ServiceCollection services = new();
            #region 注册服务
            services.AddTransient(sp => new Views.Main() { DataContext = new MainViewModel() });
            services.AddTransient<Win1>();
            services.AddTransient<ITcpClient>(sp => new TcpClient());
            services.AddDBService().RegisterTransient();
            #endregion
            return services.BuildServiceProvider();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            //ServiceProvider.GetRequiredService<IDbHelper>().Query();
            MainWindow = ServiceProvider.GetRequiredService<Win1>();
            MainWindow.Show();
        }
    }

}
